package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.SkuProperty;
import cn.wolfcode.shop.api.domain.SkuPropertyValue;
import cn.wolfcode.shop.api.service.ISkuPropertyService;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SkuPropertyValueController {

    @Reference
    private ISkuPropertyService skuPropertyService;

    @RequestMapping("skuPropertyValue/get/{id}")
    public String getSkuPropertyValueList(@PathVariable("id") Long id,Model model){
        List<SkuPropertyValue> list = skuPropertyService.getSkuPropertyValue(id);
        SkuProperty skuProperty = skuPropertyService.get(id);
        model.addAttribute("list",list);
        model.addAttribute("skuProperty",skuProperty);
        return "sku/property_value_list";
    }

    @RequestMapping("skuPropertyValue/add")
    @ResponseBody
    public JsonResultVo addSkuPropertyValue(SkuPropertyValue skuPropertyValue,@RequestParam("values") String[] values){
        JsonResultVo jsonResultVo = new JsonResultVo();
        try {
            for (String s : values) {
                skuPropertyValue.setSequence(0);
                skuPropertyValue.setValue(s);
                skuPropertyService.addSkuPropertyValue(skuPropertyValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultVo.setErrorMsg("保存失败");
        }
        return jsonResultVo;
    }

    @RequestMapping("skuPropertyValue/delete/{id}")
    @ResponseBody
    public JsonResultVo deleteValue(@PathVariable("id")Long id){
        JsonResultVo jsonResultVo = new JsonResultVo();
        try {
            skuPropertyService.deleteValue(id);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultVo.setErrorMsg("删除失败");
        }
        return jsonResultVo;
    }

}
